/**
 * 
 */
package cn.hg.solon.youcan.job.provider;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.db.PageResult;
import org.noear.solon.annotation.Component;

import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.api4j.select.Queryable;
import com.easy.query.core.api.pagination.EasyPageResult;
import com.easy.query.solon.annotation.Db;

import cn.hg.solon.youcan.common.constant.WebConstants;
import cn.hg.solon.youcan.job.entity.EqJobLog;
import cn.hg.solon.youcan.job.entity.JobLog;
import cn.hg.solon.youcan.job.service.JobLogService;

/**
 * 调度日志服务实现
 * 
 * @author 胡高
 */
@Component
public class EqJobLogProvider implements JobLogService {

    @Db("db1")
    private EasyQuery easyQuery;

    private Queryable<EqJobLog> buildQuery(Map<String, Object> paraMap) {
        String word = (String)paraMap.get("word");
        String group = (String)paraMap.get("group");
        Date startDate = (Date)paraMap.get("startDate");
        Date endDate = (Date)paraMap.get("endDate");
        String status = (String)paraMap.get("status");
        String sortField = (String)paraMap.get("sortField");
        String sortType = (String)paraMap.get("sortType");

        boolean customOrder = StrValidator.isNotBlank(sortField) && StrValidator.isNotBlank(sortType);

        // FROM sys_job_log AS t
        return this.easyQuery.queryable(EqJobLog.class)
            // WHERE t.`group` = ${group} AND t.`status` = ${status}
            //      AND ${startDate} <= t.`started_datetime` AND t.`started_datetime` <= ${endDate} 
            .where(w -> w.eq(StrValidator.isNotBlank(group), EqJobLog::getGroup, group)
                .eq(StrValidator.isNotBlank(status), EqJobLog::getStatus, status)
                .rangeClosed(ObjUtil.isNotNull(startDate) || ObjUtil.isNotNull(endDate), EqJobLog::getStartedDatetime, ObjUtil.isNotNull(startDate), startDate, ObjUtil.isNotNull(endDate), endDate)
                // AND (t.`name` LIKE '%${word}%' OR t.`target` LIKE '%${word}%' OR t.`exception` LIKE '%${word}%'
                //      OR t.`message` LIKE '%${word}%')
                .and(StrValidator.isNotBlank(word),
                    a -> a.like(EqJobLog::getName, word)
                    .or().like(EqJobLog::getTarget, word)
                    .or().like(EqJobLog::getException, word)
                    .or().like(EqJobLog::getMessage, word)))
            // ORDER BY ${sortField} DESC
            .orderBy(customOrder, ob -> ob.sqlNativeSegment(CharSequenceUtil.toUnderlineCase(sortField)),
                !CharSequenceUtil.equalsIgnoreCase(sortType, WebConstants.ORDER_DIRECTION_DESC));
    }

    @Override
    public boolean clear() {
        return this.easyQuery.deletable(EqJobLog.class).allowDeleteStatement(true).executeRows() > 0;
    }

    @Override
    public boolean delete(List<Integer> idList) {
        return this.easyQuery.deletable(EqJobLog.class).whereByIds(idList).executeRows() > 0;
    }

    @Override
    public JobLog get(Integer id) {
        return this.easyQuery.queryable(EqJobLog.class).whereById(id).firstOrNull();
    }

    @Override
    public boolean insert(JobLog bean) {
        EqJobLog cloneBean = BeanUtil.copyProperties(bean, EqJobLog.class);

        return this.easyQuery.insertable(cloneBean).executeRows() > 0;
    }

    @Override
    public PageResult<? extends JobLog> pageBy(int pageNumber, int pageSize, Map<String, Object> paraMap) {
        EasyPageResult<EqJobLog> pageList = this.buildQuery(paraMap).toPageResult(Convert.toLong(pageNumber), pageSize);

        PageResult<EqJobLog> result = new PageResult<>();
        result.addAll(pageList.getData());
        result.setTotal(Convert.toInt(pageList.getTotal()));

        return result;
    }

}
